﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "HighlightFrameCommand.h"
#include "IDocument.h"
#include "UniIdentity.h"
#include "GDFoundation/Command.h"
#include "GDDrawing/IDrawingFrame.h"
#include "UiDocumentViewUtils.h"
#include "ISelection.h"
#include "IHighlights.h"
#include "IGraphicsNodeReference.h"
#include "IGenericElement.h"
#include "IUiView.h"
#include "ICanvas.h"


using namespace gcmp;
using namespace gdmp;
namespace Sample
{
   
    bool HighlightFrameCommand::IsEnabled() const
    {
        return IsVisible();
    }

    bool HighlightFrameCommand::IsVisible() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        if (!pDocument)
            return false;

        return true;
    }

    gcmp::OwnerPtr<gcmp::IAction> HighlightFrameCommand::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
    {
        gcmp::IDocument* pDocument = GetDocument();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"断言`pDocument`失败",L"GDMPLab",L"2024-03-30");

        // 因为图框包含标题栏、会签栏、页签栏3个族实例element，在图框预选高亮、点选高亮、框选高亮、隐藏、显示时，需要将图框识别出来特殊处理，以达到图框与3个族实例整体保持一致的显示状态。
        // 此示例以点选为例，在用户点选结束后，在给高亮集合（IHighlights）添加高亮显示的图形节点时，需要从选择集中识别出图框对象进行特殊处理，以便图框整体选择高亮。
        const std::set<OwnerPtr<IGraphicsNodeReference>, OwnerPtrContainerUtil::LessPredicate<IGraphicsNodeReference>>& selectionGNodeRefs = ISelection::Get()->GetGraphicsNodeReferences();
        std::vector<OwnerPtr<IGraphicsNodeReference>> highLightGNodeRefs;

        OwnerPtrContainerUtil::AddItems(highLightGNodeRefs, selectionGNodeRefs);

        IHighlights::Get()->Clear();
        IHighlights::Get()->AddGraphicsNodeReferences(highLightGNodeRefs);

        // 补充图框的其它族实例
        FOR_EACH(item, selectionGNodeRefs)
        {
            if (!item)
            {
                continue;
            }
            const IElement* pEle = pDocument->GetElement(item->GetElementId());
            if (!pEle)
            {
                continue;
            }
            const IGenericElement* pGenElement = quick_cast<const IGenericElement>(pEle);
            if (!pGenElement)
            {
                continue;
            }

            const IExternalObject* pExternalObject = pGenElement->GetExternalObject();
            if (!pExternalObject)
            {
                continue;
            }
            const IDrawingFrame* pDrawingFrame = quick_cast<const IDrawingFrame>(pExternalObject);// 将图框识别出来
            if (!pDrawingFrame)
            {
                continue;
            }
            pDrawingFrame->HighlightInstances();// 对图框特殊处理，以达到图框整体选择高亮的效果
        }

        IUiView* pCurView = UiDocumentViewUtils::GetCurrentUiView();
        if (pCurView)
        {
            ICanvas* pCanvas = pCurView->GetCanvas();
            DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCanvas, L"断言`pCanvas`失败",L"GDMPLab",L"2024-03-30");
            pCanvas->Refresh();// 变更高亮数据后，需要刷新视图以使画布中显示生效
        }
        return nullptr;
    }

    gcmp::IDocument* HighlightFrameCommand::GetDocument() const
    {
        gcmp::IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
        if (pDocument) {
            if (!pDocument->IsFamilyDocument()) {
                return pDocument;
            }
        }
        return nullptr;
    }

    namespace
    {
        //仅作为示例展示使用，注册命令,当析构时移除注册
        gdmp::Command stub{ NEW_AS_OWNER_PTR(HighlightFrameCommand) };
    }
}
